package org.jeecg.modules.basic.entry.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import net.sf.json.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.basic.entry.entity.LwgyInoutRecord;
import org.jeecg.modules.basic.entry.service.ILwgyInoutRecordService;
import org.jeecg.modules.basic.entry.util.SenseTimeToDevice;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


/**
 * 商汤对接类
 *
 * */
@Log
@Api(tags = "SenseTime")
@RestController
@RequestMapping("/entryRest")

public class InoutConnectControllerToPubilc {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private ILwgyInoutRecordService iLwgyInoutRecordService;


    /**
     * 从商汤获取人脸设备的进出记录
     * */
    @AutoLog(value = "SenseTime-人脸识别闸机对接")
    @ApiOperation(value = "SenseTime人脸识别闸机", notes = "SenseTime人脸识别闸机")
    @PostMapping("/getOutAndInToPublic")
    @ResponseBody
    public Result<?> getOutAndIn(@RequestBody Map<String, Object> msg) {
        //SenseTime商汤数据推送开始=========
        try {
            log.info("收到一条进出消息："+msg);
            JSONObject json = JSONObject.fromObject(msg);
            System.out.println("json--:" + json);
            JSONObject data = json.getJSONObject("data");
            int data_userId = data.getInt("userId");//被识别人id
            String data_name = data.getString("userName");//被识别人姓名
            int data_type = data.getInt("type");//人员身份类型
            String data_signAvatar = data.getString("signAvatar");//识别头像
            String data_signBgAvatar = data.getString("signBgAvatar");//识别大图
            String data_icNumber = data.getString("icNumber");//被识别人ic卡号
            String data_idNumber = data.getString("idNumber");//被识别人身份证号
            String data_jobNumber = data.getString("jobNumber");//被识别人工号
            String data_remark = data.getString("remark");//备注
            int data_entryMode = data.getInt("entryMode");//核验模式（1、刷脸；2、二维码；3、刷卡；4：刷脸+刷卡；5：刷身份证；6：刷脸且刷身份证）
            int data_abnormalType = data.getInt("abnormalType");//识别记录异常类型；0-无;10001-人证不匹配;10002-人卡不匹配;10003-人码不匹配;20001-访客不在有效期内;20002-不在通行时间内；30001-无效身份证;30002-无效IC卡;30003-无效二维码;40001-体温异常;50001-未戴口罩
            int data_direction = data.getInt("direction");//进出方向
            int data_receptionUserId = data.getInt("receptionUserId");//被识别人的接待人id
            String data_receptionUserName = data.getString("receptionUserName");//被识别人的接待人姓名
            String data_deviceName = data.getString("deviceName");//设备名称
            String data_sn = data.getString("sn");//上传这条识别记录的设备LDID号
            String data_signDate = data.getString("signTime");//识别时间（秒级）
            String data_mobile = data.getString("mobile");//被识别人手机号
            String data_docPhoto = data.getString("docPhoto");//证件照片
             float data_bodyTemperature = (float) data.getDouble("bodyTemperature");//体温
             int data_mask = data.getInt("mask");//是否佩戴口罩；0 未传，1 未佩戴，2 佩戴
            String data_location = data.getString("location");//设备位置


            //时间戳转成时间
            SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Long time=Long.parseLong(data_signDate)*1000;
            String d = format.format(time);
            Date date=format.parse(d);



            LwgyInoutRecord lwgyInoutRecor = new LwgyInoutRecord();
            String id = UUIDGenerator.generate();
            lwgyInoutRecor.setId(id);
            //获取被识别人的工号data_jobNumber获取相关的studentId
            /*LwgyBaseStudent stu =iLwgyInoutRecordService.queryStuByNumString(data_jobNumber);
            System.out.println("=======获取的相关学生的id为"+stu.getId());*/

            //lwgyInoutRecor.setUserId(stu.getId()==null?data_jobNumber:stu.getId());//userId修改为学生id
            lwgyInoutRecor.setGuestId(String.valueOf(data_userId));//第三方id
            lwgyInoutRecor.setUserId(data_jobNumber);//学生或老师Id
            lwgyInoutRecor.setUserName(String.valueOf(data_name));
            lwgyInoutRecor.setDirection(String.valueOf(data_direction));
            lwgyInoutRecor.setType(String.valueOf(data_type));

            lwgyInoutRecor.setRecordDate(date);
            lwgyInoutRecor.setHeadImg(String.valueOf(data_signAvatar));
            lwgyInoutRecor.setMode(String.valueOf(data_entryMode));

            lwgyInoutRecor.setLocation(String.valueOf(data_location));
            lwgyInoutRecor.setReceiptId(String.valueOf(data_receptionUserId));
            lwgyInoutRecor.setReceiptName(String.valueOf(data_receptionUserName));
            lwgyInoutRecor.setEquipment(String.valueOf(data_deviceName));
            lwgyInoutRecor.setIdCard(String.valueOf(data_docPhoto));
            lwgyInoutRecor.setTemperature((double) data_bodyTemperature);
            lwgyInoutRecor.setIfMask(String.valueOf(data_mask));

            iLwgyInoutRecordService.save(lwgyInoutRecor);
            iLwgyInoutRecordService.powerController(lwgyInoutRecor);


            lwgyInoutRecor.setAbnormalType(String.valueOf(data_abnormalType));


            //转存到消息中间件
            CorrelationData correlationData = new CorrelationData();
            correlationData.setId(lwgyInoutRecor.getId());
            rabbitTemplate.convertAndSend("order-exchange",
                    "order.abcd",
                    lwgyInoutRecor,
                    correlationData);
        } catch (Exception e) {
            return Result.error(1, "false");
        }
        return Result.OK("success");

    }

}

